<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%String ctx = request.getContextPath();%>
<title>user list</title>
</head>

<script>
	function createClass() {
		let className = $("#classname").val();
		if(className.length === 0) {
			alert('请输入班级名称');
		} else {
			$.ajax(
				{ type:"POST",
				  url: "<%=ctx%>/clazz?act=addClass",
				  data: {
						"classname" :className
					},	
				  success: function(data, status) {
					  alert(data);
					  //刷新班级列表
					  AjaxLoad('<%=ctx%>/control?act=classOpera');
				  },
				  error: function(req, status, msg) {
					  alert(msg)
				  }
				}
			)
		}
	}

	function renameClass(className) {
		let newName = $("#"+className).val()
		if (newName.length === 0) {
			alert("请输入班级名")
		} else {
			$.ajax(
					{ type:"POST",
						url: "<%=ctx%>/clazz?act=renameClass",
						data: {
							"classname" :className,
							"newname": newName,
						},
						success: function(data, status) {
							alert(data);
							AjaxLoad('<%=ctx%>/control?act=classOpera');
						},
						error: function(req, status, msg) {
							alert(msg)
						}
					}
			)
		}
	}

	function removeClass(className) {
		
		if(!confirm('确定删除班级? ' + className)) {
			return
		}

		$.ajax(
				{ type:"POST",
				  url: "<%=ctx%>/clazz?act=removeClass",
				  data: {
						"classname" :className
					},	
				  success: function(data, status) {
					  alert(data);
					  //刷新班级列表
					  AjaxLoad('<%=ctx%>/control?act=classOpera');
				  },
				  error: function(req, status, msg) {
					  alert(msg)
				  }
				}
			)
	}
</script>


<body>
	<div id="list" class="col-md-10 col-xs-12">
		<h3>班级列表</h3>
		<hr>
		<div class="fiter col-md-10 col-xs-12">
			<form action="" class="form-horizontal form-inline">
				<input class="form-control" type="text" id="classname" placeholder="新建班级">
				<button type="button" class="btn btn-success" onclick="createClass()">创建班级</button>
			</form>
		</div>
  
<table class="table table-hover">
	<thead> <tr>
			<th>序号</th>
			<th>班级</th>
			<th>操作</th>
	</tr></thead>
	
	<tbody id="classtbody"> 
	<c:forEach items="${ClassList}" var="Class" varStatus="s">
		<tr>
				<td>${s.index + 1}</td>
				<td>
					<input class="form-control" type="text" id="${Class.name}" value="${Class.name}">
				</td>
				<td>
<button type="button" class="btn btn-primary" onclick="renameClass('${Class.name}')">
						重命名</button>
<button type="button" class="btn btn-danger" onclick="removeClass('${Class.name}')">
						删除</button>
				</td>
		</tr>
	</c:forEach>
	</tbody>
</table>
	</div>
</body>
</html>